package com.lanou.sfu.songforu.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.RecommendSongAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.datas.ReconmmendSongDatas;
import com.lanou.sfu.songforu.singleton.VolleySingleton;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by quyan on 15/10/26.
 */
public class RecommendSongListFragment extends BaseFragment implements View.OnClickListener {
    private TextView songText,tagText,singNum,collecNum,descText,songCountNum;
    private ImageView backImage;
    private CollapsingToolbarLayout collapsing_toolbar;
    private String id;
    private Toolbar toolbar;
    private VolleySingleton volleySingleton;
    private Context context;
    private ReconmmendSongDatas songDatas;
    private List<ReconmmendSongDatas.ContentEntity> datas;
    private RecommendSongAdapter adapter;
    private ListView listViewSong;
    private ProgressDialog dialog;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_songlist,null);
        songText = (TextView) view.findViewById(R.id.songListTitle);
        tagText = (TextView) view.findViewById(R.id.tagSongText);
        singNum = (TextView) view.findViewById(R.id.singSongNum);
        collecNum = (TextView) view.findViewById(R.id.collecSongNum);
        descText = (TextView) view.findViewById(R.id.descSongText);
        backImage = (ImageView) view.findViewById(R.id.backSongImag);
        listViewSong = (ListView) view.findViewById(R.id.listViewSong);
        songCountNum = (TextView) view.findViewById(R.id.songCountNum);
        //给页面设置工具栏
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);

        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        Bundle bundle = getArguments();
        id = bundle.getString("ID");
        toolbar.setNavigationIcon(R.mipmap.bt_return_nor);//返回按钮
        toolbar.setNavigationOnClickListener(this);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        initData();

    }

    public void initView(){
        //设置工具栏标题
        collapsing_toolbar = (CollapsingToolbarLayout) getActivity().findViewById(R.id.collapsing_toolbar);
        collapsing_toolbar.setContentScrimColor(0xff526bf1);//标题栏颜色
        collapsing_toolbar.setCollapsedTitleTextColor(0xffffffff);//字体颜色
        collapsing_toolbar.setExpandedTitleColor(0x00000000);//透明度 只在标题栏上显示
    }

    public void initData(){
        String url = Url.URL_SONGLIST + id;
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                songDatas = gson.fromJson(response,ReconmmendSongDatas.class);
                //设置标题文字
                tagText.setText(songDatas.getTag());//标签
                songText.setText(songDatas.getTitle());//标题
                singNum.setText(songDatas.getListenum());//听众数
                collecNum.setText(songDatas.getCollectnum());//收藏数
                descText.setText(songDatas.getDesc());//描述
                collapsing_toolbar.setTitle(songDatas.getTitle());
                Picasso.with(context).load(songDatas.getPic_300()).into(backImage);
                //歌曲数量
                songCountNum.setText(songDatas.getContent().size() + " ");
                //传数据给适配器
                datas = songDatas.getContent();
                adapter = new RecommendSongAdapter(context,datas);
                listViewSong.setAdapter(adapter);

                if (dialog.isShowing()){
                    dialog.dismiss();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        dialog = new ProgressDialog(context);
        dialog.setMessage(context.getString(R.string.loading));
        dialog.show();
        volleySingleton.addQueue(request);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }
}
